<?php

namespace App\Http\Controllers\Admin\V1;

use Illuminate\Http\JsonResponse;
use App\Extensions\Log\V1\InstanceLog;
use App\Services\Admin\V1\PermissionService;
use App\Exceptions\Admin\V1\GeneralException;
use App\Http\Resources\Admin\V1\PermissionResource;
use App\Http\Requests\Admin\V1\PermissionStoreRequest;
use App\Http\Requests\Admin\V1\PermissionUpdateRequest;
use App\Http\Requests\Admin\V1\PermissionDestroyRequest;
use Illuminate\Database\Eloquent\ModelNotFoundException;

// 控制器编号04
class PermissionController extends Controller
{
    /**
     * @var PermissionService
     */
    private $permission_service;

    /**
     * PermissionController constructor.
     * @param PermissionService $permissionService
     */
    public function __construct(PermissionService $permissionService)
    {
        $this->permission_service = $permissionService;
    }

    /**
     * @expalin 获取所有权限
     * @return JsonResponse
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/9:52
     */
    public function index()
    {
        $permissions = $this->permission_service->all();

        return $this->success(PermissionResource::collection($permissions));
    }

    /**
     * @expalin 添加权限
     * @param PermissionStoreRequest $permissionStoreRequest
     * @return JsonResponse
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/10:24
     */
    public function store(PermissionStoreRequest $permissionStoreRequest)
    {
        $parentId = (int) $permissionStoreRequest->input('parent_id');
        if ($parentId) {
            try {
                $this->permission_service->find($parentId);
            }
            catch ( ModelNotFoundException $exception ) {
                InstanceLog::instance('admin')->error('创建权限失败：' . $exception->getMessage());

                throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
            }
        }

        try {
            $this->permission_service->store($permissionStoreRequest);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('创建权限失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_SYSTEM_RETRIEVE_UNKNOWN);
        }

        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.permission.success_permission_store'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 更新权限
     * @param PermissionUpdateRequest $permissionUpdateRequest
     * @return JsonResponse
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/10:37
     */
    public function update(PermissionUpdateRequest $permissionUpdateRequest)
    {
        $parentId = (int) $permissionUpdateRequest->input('parent_id');
        if ($parentId) {
            try {
                $this->permission_service->find($parentId);
            }
            catch ( ModelNotFoundException $exception ) {
                InstanceLog::instance('admin')->error('权限更新失败：' . $exception->getMessage());

                throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
            }
        }

        try {
            $this->permission_service->update($permissionUpdateRequest->input('id'), $permissionUpdateRequest);
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('权限更新失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('权限更新失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_SYSTEM_RETRIEVE_UNKNOWN);
        }
        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.permission.success_permission_update'),
        ];

        return $this->success($result);
    }

    /**
     * @expalin 删除权限
     * @param PermissionDestroyRequest $permissionDestroyRequest
     * @return JsonResponse
     * @throws GeneralException
     * @method
     * @author 陈文小超
     * @maxim from专注to执行
     * @time 2021/4/1/10:43
     */
    public function destroy(PermissionDestroyRequest $permissionDestroyRequest)
    {
        try {
            $this->permission_service->find($permissionDestroyRequest->input('id'));
        }
        catch ( ModelNotFoundException $exception ) {
            InstanceLog::instance('admin')->error('删除权限失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_DATA_FIND__WITHOUT);
        }
        try {
            $this->permission_service->destroy($permissionDestroyRequest->input('id'));
        }
        catch ( \Exception $exception ) {
            InstanceLog::instance('admin')->error('删除权限失败：' . $exception->getMessage());

            throw new GeneralException(GeneralException::GENERAL_SYSTEM_RETRIEVE_UNKNOWN);
        }
        $result = [
            'status'  => trans('admin/v1/controller.general.success_status'),
            'message' => trans('admin/v1/controller.permission.success_permission_delete'),
        ];

        return $this->success($result);
    }
}
